﻿Imports System.IO

Public Class frmBOMChange

    Private Sub frmReminder_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
       
    End Sub


    Private Sub btn_ChooseItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ChooseItem.Click
        Dim frm As New frmItemList
        frm.IsMultiSelect = False
        frm.FilterStr = "ItemCode in (Select T0.Code from ITT1 T0 join OITT T1 on T0.Father=T1.Code where T1.TreeType='P')"
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtItemCodeFr.Text = frm.GridDetail.GetValue("ItemCode")
            txtItemNameFr.Text = frm.GridDetail.GetValue("ItemName")
            txtAvgPriceFr.Text = frm.GridDetail.GetValue("AvgPrice")
            txtLastPriceFr.Text = frm.GridDetail.GetValue("LastPurPrc")

            Dim fn As New Functions
            Dim str As String
            Dim dt As DataTable
            str = "select T0.Quantity CurrentQty,T2.ItemCode, T2.ItemName from ITT1 T0 join OITT T1 on T0.Father=T1.Code join OITM T2 on T2.ItemCode=T1.Code where T0.Code='" + txtItemCodeFr.Text + "'"
            dt = fn.DoQueryReturnDT(str)
            GridDetail.DataSource = dt
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Close.Click
        Me.Dispose()
    End Sub

    Private Sub btn_DoReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_DoReplace.Click
        If txtItemCodeTo.Text = "" Then
            lbl_Message.Text = "Please select an item to replace"
            Return
        End If
        Dim oBOM As SAPbobsCOM.ProductTrees
        oBOM = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oProductTrees)
        If GridDetail.RowCount = 0 Then
            lbl_Message.Text = "There is no item to replace"
            Return
        End If

        For i As Integer = 0 To GridDetail.RowCount - 1
            GridDetail.Row = i
            If oBOM.GetByKey(GridDetail.GetValue("ItemCode")) Then
                For j As Integer = 0 To oBOM.Items.Count - 1
                    oBOM.Items.SetCurrentLine(j)
                    Dim itCode As String = oBOM.Items.ItemCode
                    Dim itCodeFr As String = txtItemCodeFr.Text
                    If oBOM.Items.ItemCode = txtItemCodeFr.Text Then
                        oBOM.Items.ItemCode = txtItemCodeTo.Text
                        oBOM.Items.Quantity = GridDetail.GetValue("NewQty")
                        Exit For
                    End If
                Next
                If oBOM.Update() <> 0 Then
                    lbl_Message.Text = "Updating failed. [" + oBOM.txtItemCodeFr.Text + "]"
                Else
                    lbl_Message.Text = "Update completed"
                End If
            End If
        Next
    End Sub

    Private Sub btn_ChooseReplacedItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ChooseReplacedItem.Click
        Dim frm As New frmItemList
        frm.IsMultiSelect = False
        frm.FilterStr = "ItemCode not in (Select Code from OITT)"
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtItemCodeTo.Text = frm.GridDetail.GetValue("ItemCode")
            txtItemNameTo.Text = frm.GridDetail.GetValue("ItemName")
            txtAvgPriceTo.Text = frm.GridDetail.GetValue("AvgPrice")
            txtLastPriceTo.Text = frm.GridDetail.GetValue("LastPurPrc")
        End If
    End Sub
End Class
